package ce.com.newbluedemo.k2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import ce.com.cenewbluesdk.entity.k2.K2_SendGoal;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/28.
 */

public class ActivitySendGoal extends K2BaseActivity implements View.OnFocusChangeListener {
    private EditText mEtSteps;
    private EditText mEtDistance;
    private EditText mEtCal;
    private EditText mEtSleep;
    private int mSteps;
    private int mDistance;
    private int mCal;
    private int mSleep;

    private CEDevK2Proxy k2Proxy;

    private Handler mK2Handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                showToast("发送失败，请稍后重试！");
            } else {
                mEtCal.setText("");
                mEtDistance.setText("");
                mEtSleep.setText("");
                mEtSteps.setText("");
                showToast("发送成功！");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_goal);
        setTitle("发送目标");
        k2Proxy = BleFactory.getInstance().getK2Proxy();
        k2Proxy.setHandler(mK2Handler);
        mEtSteps = (EditText) findViewById(R.id.et_steps);
        mEtSteps.setOnFocusChangeListener(this);
        mEtDistance = (EditText) findViewById(R.id.et_distance);
        mEtDistance.setOnFocusChangeListener(this);
        mEtCal = (EditText) findViewById(R.id.et_cal);
        mEtCal.setOnFocusChangeListener(this);
        mEtSleep = (EditText) findViewById(R.id.et_sleep);
        mEtSleep.setOnFocusChangeListener(this);
        findViewById(R.id.btn_send_goal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!k2Proxy.isConnectOK()) {
                    showToast("蓝牙已经断开");
                    return;
                }
                k2Proxy.getSendHelper().sendGoal(new K2_SendGoal(mSteps, mDistance, mCal, mSleep));

            }
        });
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) return;
        switch (v.getId()) {
            case R.id.et_steps:
                String inputSteps = mEtSteps.getText().toString();
                if (LittleUtils.isInteger(inputSteps) || TextUtils.isEmpty(inputSteps)) {
                    mSteps = Integer.parseInt(inputSteps);
                } else {
                    showToast("输入有误，请重新输入");
                }
                break;
            case R.id.et_distance:
                String inputDistance = mEtSteps.getText().toString();
                if (LittleUtils.isInteger(inputDistance) || TextUtils.isEmpty(inputDistance)) {
                    mDistance = Integer.parseInt(inputDistance);
                } else {
                    showToast("输入有误，请重新输入");
                }
                break;
            case R.id.et_cal:
                String inputCal = mEtSteps.getText().toString();
                if (LittleUtils.isInteger(inputCal) || TextUtils.isEmpty(inputCal)) {
                    mCal = Integer.parseInt(inputCal);
                } else {
                    showToast("输入有误，请重新输入");
                }
                break;
            case R.id.et_sleep:
                String inputSleep = mEtSteps.getText().toString();
                if (LittleUtils.isInteger(inputSleep) || TextUtils.isEmpty(inputSleep)) {
                    mSleep = Integer.parseInt(inputSleep);
                } else {
                    showToast("输入有误，请重新输入");
                }
                break;
            default:
                break;
        }
    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }
}
